﻿using System;
using Nest;
using MongoDB.Bson;
using MongoDB.Driver;
using Elasticsearch.Synchronize.Job.Settings;

namespace Elasticsearch.Synchronize.Job.Job
{
    public abstract class BaseProcessor<TModel> where TModel : class
    {
        protected readonly ElasticClient _elasticClient;

        protected readonly IMongoCollection<TModel> _mongoCollection;

        protected readonly IMongoCollection<BsonDocument> _fullMongoCollection;
        
        protected BaseProcessor(SettingOption option)
        {
            var mongoClient = new MongoClient(option.MongoServer);
            var mongoDatabase = mongoClient.GetDatabase(option.MongoDatabase);
            _mongoCollection = mongoDatabase.GetCollection<TModel>(option.MongoCollection);
            _fullMongoCollection = mongoDatabase.GetCollection<BsonDocument>(option.MongoCollection);

            var settings = new ConnectionSettings(new Uri(option.ElasticsearchUrl));
            settings.DisableDirectStreaming();
            settings.DefaultFieldNameInferrer(f => f);
            _elasticClient = new ElasticClient(settings);
        }
    }
}